// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: "yiliaojiufeng-6gt0kj1l1df8b7b7",
    traceUser: true,
})

const time_format = (time) => {
    if (time.toString().length == 13) {
        var tme = new Date(time);
    } else {
        var tme = new Date(time * 1000);
    }
    var Y = tme.getFullYear();
    var M = (tme.getMonth() + 1 < 10 ? '0' + (tme.getMonth() + 1) : tme.getMonth() + 1);
    var D = tme.getDate();
    var h = tme.getHours();
    var m = tme.getMinutes();
    var tem1 = Y + '-' + M + '-' + D + ' ' + h + ':' + m;
    console.log(tem1);
    return tem1;
}

// 云函数入口函数
const db = cloud.database()

exports.main = async (event, context) => {
    var newsDetail = {}
    var time = ''
    var html = ''
    //获取文章信息
    await db.collection('officialInfo').where({
        _id: event.newsId
    }).limit(1).get().then(res => {
        newsDetail = res.data[0]
        time = time_format(res.data[0]._createTime)
        html = res.data[0].richText.replace(/<p/ig, '<p class="p_class"')
    })

    return {
        time: time,
        mainpic: newsDetail.mainpic,
        title: newsDetail.title,
        _id: newsDetail._id,
        html: html,
    }
}